package com.gulimall.product.app;

import com.common.utils.PageUtils;
import com.common.utils.R;
import com.gulimall.product.entity.AttrEntity;
import com.gulimall.product.entity.AttrGroupEntity;
import com.gulimall.product.service.AttrAttrgroupRelationService;
import com.gulimall.product.service.AttrGroupService;
import com.gulimall.product.service.AttrService;
import com.gulimall.product.service.CategoryService;
import com.gulimall.product.vo.AttrGroupRelationVo;
import com.gulimall.product.vo.AttrGroupWithAttrsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-04-15 14:26:28
 */
@Slf4j
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{attId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("attId") Long attId) {
        /* PageUtils page = attrGroupService.queryPage(params); */
        PageUtils page = attrGroupService.queryPage(params, attId);
        return R.ok().put("page", page);
    }

    /**
     * 获取分类下所有的分组及每个分组的关联属性
     * /product/attrgroup/{catelogId}/withattr
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupAndAttr(@PathVariable("catelogId")Long cateLogId){
        List<AttrGroupWithAttrsVo> list= attrGroupService.getAttrGroupAndAttr(cateLogId);
        return R.ok().put("data",list);
    }


    /**
     * 添加关联关系
     * @param groupId
     * @return
     * /product/attrgroup/attr/relation
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> relationVoList ){
        relationService.saveBatch(relationVoList);
        return R.ok();
    }

    @RequestMapping("/{groupId}/attr/relation")
    public R getRelation(@PathVariable("groupId") Long groupId) {
        List<AttrEntity> data = attrService.getRelationAttr(groupId);
        return R.ok().put("data", data);
    }

    ///product/attrgroup/{attrgroupId}/noattr/relation
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R getNoRelation(@PathVariable("attrgroupId") Long attrgroupId, @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoRelationAttr(attrgroupId, params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] parentIds = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(parentIds);
        System.out.println("穿回去的数据：" + attrGroup);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] relationEntities) {
        attrService.deleteRelation(relationEntities);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
